package com.easyctba.core.controller;

import java.util.List;

import com.easyctba.core.domain.bo.FiasAddPolylineBo;
import com.easyctba.core.domain.bo.FiasCategoryAddBo;
import com.easyctba.core.domain.bo.FiasClassAddBo;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.easyctba.common.idempotent.annotation.RepeatSubmit;
import com.easyctba.common.log.annotation.Log;
import com.easyctba.common.web.core.BaseController;
import com.easyctba.common.mybatis.core.page.PageQuery;
import com.easyctba.common.core.domain.R;
import com.easyctba.common.core.validate.AddGroup;
import com.easyctba.common.core.validate.EditGroup;
import com.easyctba.common.log.enums.BusinessType;
import com.easyctba.common.excel.utils.ExcelUtil;
import com.easyctba.core.domain.vo.CtbaFiasClassVo;
import com.easyctba.core.domain.bo.CtbaFiasClassBo;
import com.easyctba.core.service.ICtbaFiasClassService;
import com.easyctba.common.mybatis.core.page.TableDataInfo;

/**
 * fias分类
 * 前端访问路由地址为:/core/fiasClass
 *
 * @author Huang
 * @date 2024-03-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/fiasClass")
public class CtbaFiasClassController extends BaseController {

    private final ICtbaFiasClassService ctbaFiasClassService;

    /**
     * 查询fias分类列表
     */
    @SaCheckPermission("core:fiasClass:list")
    @GetMapping("/list")
    public TableDataInfo<CtbaFiasClassVo> list(CtbaFiasClassBo bo, PageQuery pageQuery) {
        return ctbaFiasClassService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出fias分类列表
     */
    @SaCheckPermission("core:fiasClass:export")
    @Log(title = "fias分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CtbaFiasClassBo bo, HttpServletResponse response) {
        List<CtbaFiasClassVo> list = ctbaFiasClassService.queryList(bo);
        ExcelUtil.exportExcel(list, "fias分类", CtbaFiasClassVo.class, response);
    }

    /**
     * 获取fias分类详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("core:fiasClass:query")
    @GetMapping("/{id}")
    public R<CtbaFiasClassVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(ctbaFiasClassService.queryById(id));
    }

    /**
     * 新增fias分类
     */
    @SaCheckPermission("core:fiasClass:add")
    @Log(title = "fias分类", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CtbaFiasClassBo bo) {
        return toAjax(ctbaFiasClassService.insertByBo(bo));
    }

    /**
     * 修改fias分类
     */
    @SaCheckPermission("core:fiasClass:edit")
    @Log(title = "fias分类", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CtbaFiasClassBo bo) {
        return toAjax(ctbaFiasClassService.updateByBo(bo));
    }

    /**
     * 删除fias分类
     *
     * @param ids 主键串
     */
    @SaCheckPermission("core:fiasClass:remove")
    @Log(title = "fias分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(ctbaFiasClassService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 新增编码
     * @param bo 大类业务对象
     * @return NULL
     */
    @PostMapping("/front")
    public R<Void> addClass(@Validated(AddGroup.class) @RequestBody FiasClassAddBo bo){
        return ctbaFiasClassService.addClass(bo);
    }

    /**
     * 删除分类
     */
    @DeleteMapping("/front/{id}")
    public R<Void> delete(@NotNull(message = "FIAS编码分类id不能为空") @PathVariable Long id){
        return ctbaFiasClassService.delete(id);
    }

    /**
     * 根据id获取Class对应的num
     */
    @GetMapping("/num/{id}")
    public R<String> getNum(@NotNull(message = "FIAS编码分类id不能为空") @PathVariable Long id){
        return ctbaFiasClassService.getNum(id);
    }

    @GetMapping("/front/{id}")
    public R<CtbaFiasClassVo> getInfoFront(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(ctbaFiasClassService.queryById(id));
    }


}
